package com.qm.controller;

import com.qm.pojo.Item;
import com.qm.pojo.ItemDesc;
import com.qm.vo.LayUITable;
import com.qm.vo.PageResult;
import com.qm.vo.QmResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qm.service.ItemService;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/item")
public class ItemController {
	
	@Autowired
	private ItemService itemService;

	/**
	 * 业务说明: 根据分页实现商品查询
	 * URL地址:	http://localhost:8091/item/query?page=1&rows=20
	 * 参数:	page/rows
	 * 返回值:  LayUITable
	 */
	@RequestMapping("/query")
	public PageResult findItemByPage(Integer page, Integer limit) {
		LayUITable data = itemService.findItemByPage(page, limit);
		return new PageResult().success(data.getRows() ,data.getTotal());
	}
	
	@RequestMapping("/save")
	public QmResult saveItem(Item item, ItemDesc itemDesc) {
		try {
			itemService.saveItem(item,itemDesc);
			return QmResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return QmResult.fail();
		}
	}

	@RequestMapping("/update")
	public QmResult updateItem(Item item, ItemDesc itemDesc) {
		itemService.updateItem(item,itemDesc);
		return QmResult.success();
	}

	@RequestMapping("/delete")
	public QmResult deleteItems(Long... ids){
		itemService.deleteItems(ids);
		return QmResult.success();
	}

	/**
	 * 实现上架/下架通用操作
	 * 	url: /item/reshelf   上架操作 status=1
	 * 	url: /item/instock   下架操作 status=2
	 * 	restFUl操作方法
	 * 	URL优化: url: /item/updateStatus/2
	 */
	@RequestMapping("/updateStatus/{status}")
	public QmResult instockItems(@PathVariable Integer status, Long... ids) {
		itemService.updateStatus(status,ids);
		return QmResult.success();
	}

	/**
	 * 根据id查询商品分类名称
	 * URL地址：/item/query/item/desc/data.id
	 * 参数：id
	 * 返回值：SysResult对象（itemDesc对象）
	 */
	@RequestMapping("/query/item/desc/{id}")
	public QmResult findItemDescById(@PathVariable Long id) {
		ItemDesc itemDesc = itemService.findItemDescById(id);
		return QmResult.success(itemDesc);
	}

	@RequestMapping("/query/item/{id}")
	public QmResult findItemById(@PathVariable Long id) {
		Item item = itemService.findItemById(id);
		return QmResult.success(item);
	}

}
